﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO.Common;
using DAO.Common;
using System.Data.Common;
using System.Data;
using System.Transactions;
using System.ServiceModel;
using ServiceInterfaces.Stores;

namespace DAO.Headquarter
{
    public class ProductDAO : DAO.Common.DAO
    {

        public ProductDAO(string provider, string connStr) : base(provider, connStr) { }

        public void AddProduct(Product product) {

            using (TransactionScope ts = new TransactionScope(
                TransactionScopeOption.Required, 
                new TransactionOptions{ IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
            {

                //Adds the product to headquarted db
                using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
                {
                    DbCommand command = DBHelper.CreateCommandWithParameters(
                        conn,
                        "addProduct",
                        CommandType.StoredProcedure,
                        product.ToParameterList());

                    conn.Open();
                    product.Code = (int)command.ExecuteScalar();
                }

                // Add the product to its respective store
                ChannelFactory<IStoresManagementMSMQ> store = ChannelFactoryStoresHelper.CreateChannelFactoryToStores<IStoresManagementMSMQ>(Operation.ADD, product.Type);
                IStoresManagementMSMQ storeManagement = store.CreateChannel();
                storeManagement.AddProduct(product);
                store.Close();

                ts.Complete();
            }
        }

        //public Product GetProduct(int productCode) {

        //    Product product = null;
        //    using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
        //    {
        //        DbCommand command = DBHelper.CreateCommandWithParameters(
        //            conn,
        //            "",
        //            CommandType.StoredProcedure,
        //            new List<ParameterHelper> { new ParameterHelper{ DbType = DbType.Int32, Name = "code", Value = productCode } });

        //        using (DbDataReader reader = command.ExecuteReader())
        //        {
        //            product = new Product();
        //            product.FromDataReader(reader);
        //        }
        //    }

        //    return product;
        //}

        public void ProductSold(int productCode, int amount) {

            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand command = DBHelper.CreateCommandWithParameters(
                    conn,
                    "decreaseProductStock",
                    CommandType.StoredProcedure,
                    new List<ParameterHelper> { 
                        new ParameterHelper { DbType = DbType.Int32, Name = "code", Value = productCode },
                        new ParameterHelper { DbType = DbType.Int32, Name = "amount", Value = amount }
                    });

                conn.Open();
                command.ExecuteNonQuery();
            }
        }

    }
}
